package com.chiyi.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.chiyi.bean.TProduct;
import com.chiyi.service.ProductClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Random;

/**
 * @date 2021/5/13 17:40
 */
@RestController
@Api(tags = "测试模块")
@Slf4j
@RequestMapping("/test")
public class TestController {

    @Resource
    private ProductClient productClient;


    @ApiOperation("work")
    @GetMapping("/work")
    public String work() {
        return "success";
    }


    @ApiOperation("连接商品服务测试")
    @GetMapping("/connectProduct")
    public String connectProduct() {
        return productClient.work();

    }

    @ApiOperation("高并发场景")
    @GetMapping("/highConcurrent/{pid}")
    public TProduct highConcurrent(@PathVariable("pid") Integer pid) {
        TProduct tProduct = productClient.detail(pid);
        log.info("商品信息：{}", tProduct);
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return tProduct;
    }

    @ApiOperation("消息")
    @GetMapping("/message")
    public String message() {
        return "高并发...";
    }

    @ApiOperation("message1")
    @GetMapping("/message1")
    public String message1() {
        log.info("message1");
        return "message1...";
    }

    @ApiOperation("user")
    @GetMapping("/user")
    @SentinelResource("user")
    public String user(String name, Integer age) {
        log.info("姓名：{},年龄：{}", name, age);
        return String.format("姓名：%s,年龄：%s", name, age);
    }

    @ApiOperation("message3")
    @GetMapping("/message3")
    public String message3() {
        int i = new Random().nextInt(10);
        log.info("{}", i);
        if (i % 3 != 0) {
            log.info("主动抛出异常");
            throw new RuntimeException();
        }
        return "message3...";
    }
}
